package com.zz.parkingsystem.service.impl;

import com.zz.parkingsystem.entity.*;
import com.zz.parkingsystem.mapper.*;
import com.zz.parkingsystem.service.CarService;
import com.zz.parkingsystem.service.OrderItemService;
import com.zz.parkingsystem.service.OrderService;
import com.zz.parkingsystem.service.ParkingService;
import com.zz.parkingsystem.util.ComputeUtil;
import com.zz.parkingsystem.util.NumberUtil;
import com.zz.parkingsystem.vo.ShowVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.zz.parkingsystem.dto.aaa;
import com.zz.parkingsystem.entity.OrderItem;
import com.zz.parkingsystem.mapper.OrderItemMapper;
import com.zz.parkingsystem.service.OrderItemService;
import com.zz.parkingsystem.vo.OrderItemVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import com.zz.parkingsystem.entity.OrderItem;
import com.zz.parkingsystem.mapper.OrderItemMapper;
import com.zz.parkingsystem.service.OrderItemService;
import com.zz.parkingsystem.vo.AllOrderitemBySectionVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.crypto.Data;

@Service
public class OrderItemServiceImpl implements OrderItemService {
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private ParkingService parkingService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private CarService carService;
    @Autowired
    private StrategyMapper strategyMapper;
    @Autowired
    private WeekdayMapper weekdayMapper;
    @Autowired
    private WeekendMapper weekendMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SectionMapper sectionMapper;
    @Autowired
    private AppealPayMapper appealPayMapper;
    @Autowired
    private AppealUnpaidMapper appealUnpaidMapper;
    @Autowired
    private ParkingMapper parkingMapper;
    @Autowired
    private CarMapper carMapper;
    @Override
    public int deleteByPrimaryKey(Long id) {
        return orderItemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(OrderItem record) {
        return orderItemMapper.insert(record);
    }

    @Override
    public int insertSelective(OrderItem record) {
        return orderItemMapper.insertSelective(record);
    }

    @Override
    //用户停车，创建订单项
    public String insertSelectiveUser(OrderItem record) {
        //判断是否是无效泊位号（没有此泊位）
        Parking parking = parkingService.selectByPrimaryKey(record.getPkId().longValue());
        if(parking==null){
            return "没有找到此泊位号";
        }
        //判断该泊位是否被占用
        Order order = orderService.selOrderByStatusByPkId(record.getPkId());
        if(order!=null){
            return "该泊位号被占用(该泊位上有订单状态为进行中的订单)";
        }
        if(parking.getPkStatus()==1){
            return "该泊位号被占用（该泊位的车位状态显示有车）";
        }
        //如果泊位号有效且泊位没有被占用，更改泊位状态为有车
        parking.setPkStatus(1);
        parkingService.updateByPrimaryKey(parking);
        //判断是否没有检测到车辆
        if(record.getCarId()==null || carService.selectByPrimaryKey(record.getCarId().longValue())==null){
            return "没有检测到车辆";
        }
        //订单项订单流水号
        String oi_no = NumberUtil.genOrderNo();
        record.setOiNo(oi_no);
        //订单项开始时间
        record.setOiStartTime(new Date());
        orderItemMapper.insertSelective(record);
        //再生成订单
        OrderItem orderItem = orderItemMapper.selOrderItemByOiNo(oi_no);
        Order order1 = new Order();
        order1.setOiId(orderItem.getOiId());
        order1.setOrderStatus(0);//进行中
        orderService.insertSelectiveUser(order1);
        return "订单项创建成功，用户可以认领订单了"+"!"+oi_no;
    }
    @Override
    public OrderItem selectByPrimaryKey(Long id) {
        return orderItemMapper.selectByPrimaryKey(id);
    }
    @Override
    //用户车辆离开后,产生离开时间，计算金额,修改订单状态为未支付，修改泊位状态为无车
    public int updateByPrimaryKeySelectiveUser(OrderItem record) {
        record.setOiEndTime(new Date());
        //这里注意，前端传入的orderItem里的int预留列里要放入主策略id
        //请注意，请注意！
        Strategy strategy = strategyMapper.selectByPrimaryKey(record.getReservedInt().longValue());
        //创建工具类对象
        ComputeUtil computeUtil = new ComputeUtil();
        //订单项加金额
        BigDecimal money = computeUtil.computeMoney(record.getOiStartTime(), record.getOiEndTime(), strategy, weekdayMapper, weekendMapper);
        record.setOiAmount(money);
        //将停车时间放入预留列
        record.setReservedVar(computeUtil.computeTimeByStartAndEnd(record.getOiStartTime(),record.getOiEndTime()));
        //修改订单状态为未支付
        Order order = orderService.selOrderByStatusByPkId(record.getPkId());
        order.setOrderStatus(1);
        orderMapper.updateByPrimaryKeySelective(order);
        //修改泊位状态为无车
        Parking parking = parkingService.selectByPrimaryKey(record.getPkId().longValue());
        parking.setPkStatus(0);
        parkingService.updateByPrimaryKeySelective(parking);
        //将产生的金额放入对应的账户表的未支付金额中
        Account account = accountMapper.selectByUserId(record.getUserId());
        account.setAccUnpaid(account.getAccUnpaid().add(money));
        accountMapper.updateByPrimaryKeySelective(account);
        return orderItemMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(OrderItem record) {
        return orderItemMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeySelective(OrderItem record) {
        return orderItemMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    //查该用户是否有没有支付的订单
    public OrderItem selOrderItemByUserIdAndUnpaid(Long userId) {
        return orderItemMapper.selOrderItemByUserIdAndUnpaid(userId);
    }

    @Override
    //通过订单流水号查订单项
    public OrderItem selOrderItemByOiNo(String oiNo) {
        return orderItemMapper.selOrderItemByOiNo(oiNo);
    }

    @Override
    //通过订单流水号查模板消息和订单缴费中的内容，确定其中的功能,封装成vo返回
    public ShowVO show(String oiNo) {
        ShowVO showVO = new ShowVO();
        showVO.setOiNo(oiNo);
        OrderItem orderItem = orderItemMapper.selOrderItemByOiNo(oiNo);
        showVO.setOiAmount(orderItem.getOiAmount());
        Car car = carService.selectByPrimaryKey(orderItem.getCarId().longValue());
        showVO.setCarLicensePlate(car.getCarLicensePlate());
        Parking parking = parkingService.selectByPrimaryKey(orderItem.getPkId().longValue());
        showVO.setPkNo(parking.getPkNo());
        Section section = sectionMapper.selectByPrimaryKey(orderItem.getSecId().longValue());
        showVO.setSecName(section.getSecName());
        showVO.setOiStartTime(orderItem.getOiStartTime());
        showVO.setStopTime(orderItem.getReservedVar());
        Order order = orderMapper.selOrderByOrderItemId(orderItem.getOiId());

        //进行中
        if(order.getOrderStatus()==0){
            showVO.setState(1);
        }
        //待支付
        if(order.getOrderStatus()==1){
            showVO.setState(2);
        }
        //已支付
        if(order.getOrderStatus()==2){
            showVO.setState(3);
        }
        AppealPay appealPay = appealPayMapper.selByOiId(orderItem.getOiId());
        AppealUnpaid appealUnpaid = appealUnpaidMapper.selByOiId(orderItem.getOiId());
//        if(appealPay==null){
//            showVO.setState(3);
//        }
//        if(appealUnpaid==null){
//            showVO.setState(3);
//        }
        if(appealPay!=null ){
            //申诉中
            if(appealPay.getApStatus()==0 ){
                showVO.setState(4);
            }
            //申诉失败
            if(appealPay.getApStatus()==2 ){
                showVO.setState(5);
            }
            //申诉成功
            if(appealPay.getApStatus()==1 ){
                showVO.setState(6);
            }
        }
        return showVO;
    }

    @Override
    public List<OrderItem> selAllOrderItemByUserId(Integer userId) {
        return orderItemMapper.selAllOrderItemByUserId(userId);
    }
    //使用中
    //用户输入泊位号,和车牌，判断该泊位号的情况（是否车牌为空，是否泊位被占用，是否无效泊位号）
    @Override
    public String selParkingByPkNo(Integer pkNO, String carLicensePlate,Integer accId) {
        Parking parking = parkingMapper.selByCarLicensePlate(pkNO);
        if(parking==null){
            return "无效的泊位号";
        }
        if(parking.getPkStatus()==1){
            return "该泊位号被占用";
        }
        if(parking.getPkStatus()==-1){
            return "该泊位未激活";
        }
        if("".equals(pkNO)){
            return "没有检测到车牌";
        }
        Car car = carMapper.selCarBycarLicensePlate(carLicensePlate);
        if(car==null){
            return "该车辆不存在";
        }
        if(car.getAccId()!=accId){
            return "您没有绑定该车辆";
        }
        return "无异常认领订单";
    }

    @Override
    public List<OrderItemVo> getPaymentAmount() {
        return orderItemMapper.getPaymentAmount();
    }

    @Override
    public List<OrderItem> getOrdersTotal() {
        return orderItemMapper.getOrdersTotal();
    }

    @Override
    public OrderItemVo getUnrecorded(String oiNo) {
        return orderItemMapper.getUnrecorded(oiNo);
    }

    @Override
    public List<OrderItem> getUnrecordedVehicle(int pkno) {
        return orderItemMapper.getUnrecordedVehicle(pkno);
    }

    @Override
    public List<OrderItem> selectByCarname(String carLicensePlate) {
        return orderItemMapper.selectByCarname(carLicensePlate);
    }


    @Override
    public List<AllOrderitemBySectionVo> getselAllBySection(Integer order_status) {
        return orderItemMapper.selAllBySection(order_status);
    }

    @Override
    public AllOrderitemBySectionVo getselOneOrderItem(String oi_no) {
        return orderItemMapper.selOneOrderItem(oi_no);
    }

    @Override
    public OrderItem getselImg(String oi_no) {
        return orderItemMapper.selImg(oi_no);
    }

    @Override
    public int getselOrderSum() {
        return orderItemMapper.selOrderSum();
    }

    @Override
    public int getselRefundSum() {
        return orderItemMapper.selRefundSum();
    }

    @Override
    public double getselMoneySum() {
        return orderItemMapper.selMoneySum();
    }

    @Override
    public int getSumNumberBytime(Date oi_start_time) {
        return orderItemMapper.SumNumberBytime(oi_start_time);
    }

    @Override
    public double getSumMoneyBytime(Date oi_start_time) {
        return orderItemMapper.SumMoneyBytime(oi_start_time);
    }

}
